/**
 * @author caojj
 * @version 1.0.5.5
 * 		 
 * @history
 * 			1.0.1.1 + 创建input时的兼容性 @20080716
 * 			1.0.2.0 + 递归创建子节点
 * 			1.0.3.0	+ clearChildNodes
 * 					# 特殊属性过滤
 * 					+ 附加子节点
 * 			1.0.4.0 + 将bindDom 与 childDomsAttr的参数合并，兼容FWK.createElement的写法
 * 			1.0.4.2 + 属性附加text
 * 			1.0.5.0 + 根节点绑定ref引用机制 @20081029
 * 			1.0.5.4 + 属性可以是Dom引用 @20081127
 */

try {
	if(!FWK.Util){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.Util = {}
}


/**
 * 
 * @param {Object} tagName
 * @param {Object} attrs
 * @param {Attr}	childDomsAttr	子节点属性集合
 * @param {Object} bindDom
 * @param {Object} ref	返回变量引用，必须为object或array
 */
FWK.Util.createElement = function(tagName, attrs, childDomsAttr, bindDom, ref) {
	var dom = null;
	if (!ref) {
		ref = FWK.Util.createElement.bindRefCache;
	}
	FWK.Util.createElement.level++;
//	if (childDomsAttr) {
//		attrs["_childDoms"] = _childDoms;
//	}
	
	if (tagName=="input" && document.all) {
		var iptStr = "<"+tagName;
		if (attrs["name"]) {
			iptStr += " name=" + attrs["name"];
		}
		if (attrs["type"]) {
			iptStr += " type=" + attrs["type"];
		}
		if (attrs["hideFocus"]) {
			iptStr += " hideFocus=" + attrs["type"];
		}
		
		iptStr += ">";
		dom = document.createElement(iptStr);
	} else {
		dom = document.createElement(tagName);
	}
	
	for (var aName in attrs) {
		var attr = attrs[aName];

		if (typeof(attr) == "object") {
			if (aName == "style") {
				for (var styleName in attr) {
					dom.style[styleName] = attr[styleName].replace(/;$/,"");
				}
			} else {
				dom[aName] = attr;
			}
		} else {
//			if (typeof(attr) == "string" && (aName != "className" && aName != "innerHTML" && aName != "textContent")) {
			var matchReg = new RegExp("\\b"+aName+"\\b", "i"); 	//eval("/\\b"+aName+"\\b/");
			if (typeof(attr) == "string" && (!FWK.Util.createElement.filter.match(matchReg))) {
				dom.setAttribute(aName, attr);
			} else {
				dom[aName] = attr;
			}
			
			if (attr && (FWK.Util.createElement.extra.match(matchReg))) {
				switch(aName) {
					case "text":
						dom.appendChild(document.createTextNode(attr));
						break;
					case "ref":
						break;
					default:
						break;
				}
			}
		}
	}
	
	if (childDomsAttr) {
		if (childDomsAttr instanceof Array) {
			for (var j=0; j<childDomsAttr.length; j++) {
				if (childDomsAttr[j] && (childDomsAttr[j].nodeType == 1 || childDomsAttr[j].nodeType == 3)) {		//如果当前是节点
					dom.appendChild(childDomsAttr[j]);
				} else {	//正常情况参数数组
					var tmpDom = FWK.Util.createElement(childDomsAttr[j][0], childDomsAttr[j][1], childDomsAttr[j][2], dom, ref);
					if (childDomsAttr[j][1] && childDomsAttr[j][1]["ref"] && ref) {
						ref[childDomsAttr[j][1]["ref"]] = tmpDom;
					}
					delete tmpDom;
				}
			}
		} else {	//如果第四个参数是dom
			childDomsAttr.appendChild(dom);
		}		
	}
	
	if (bindDom) {
		bindDom.appendChild(dom);
	}
	FWK.Util.createElement.level--;
	if (FWK.Util.createElement.level<1) {
//		dom.doms={dom:dom}
		dom.refs = {};
		for (var i in FWK.Util.createElement.bindRefCache) {
			dom.refs[i] = FWK.Util.createElement.bindRefCache[i];
		}
		FWK.Util.createElement.bindRefCache = {};
	}
	return dom;
}
FWK.Util.createElement.filter = "className, innerHTML, textContent";
FWK.Util.createElement.extra = "text, ref";
FWK.Util.createElement.bindRefCache = {};
FWK.Util.createElement.level = 0;

/**
 * 清除dom所有子节点
 * @param {Object} dom
 */
FWK.Util.clearChildNodes = function(dom) {
	while (dom.lastChild) {
		var tmpDom = dom.removeChild(dom.lastChild);
		if (tmpDom.refs) {		//used in IE
			for (var i in tmpDom.refs) {
				if (tmpDom.refs[i].parentNode) {
					var tmpChildDom = tmpDom.refs[i].parentNode.removeChild(tmpDom.refs[i]);
					delete tmpChildDom;
					delete tmpDom.refs[i];
				}
			}
		}
		delete tmpDom;
	}
}
